﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using Entity;
using Command;
namespace DAL
{
    public class L_AdminDAL
    {
        //查询管理员信息
        public static List<UserInfo> SelectAdminMessage()
        {
            string sql = string.Format("select*from UserInfo  where UserPower='1'and UserState='1'");
            SqlDataReader sdr = DBHelper.Getdata(sql);
            List<UserInfo> UI = new List<Entity.UserInfo>();
            while (sdr.Read())
            {
                UserInfo f = new UserInfo();
                f.UserName = (string)sdr["UserName"];
                f.UserPwd = (string)sdr["UserPwd"];
                f.UserPower = (int)sdr["UserPower"];
                f.UserId = (int)sdr["UserId"];
                UI.Add(f);
            }
            sdr.Close();
            return UI;
        }
        //修改管理员信息
        public static List<UserInfo> UpdateMessage(UserInfo UserInfo)
        {
            string sql = string.Format("select * from UserInfo where UserId={0}", UserInfo.UserId);
            SqlDataReader sdr = DBHelper.Getdata(sql);
            List<UserInfo> list = new List<UserInfo>();
            while (sdr.Read())
            {
                UserInfo f = new UserInfo();
                f.UserId = Convert.ToInt32(sdr["UserId"]);
                f.UserName = (string)sdr["UserName"].ToString();
                f.UserPwd = (string)sdr["UserPwd"];
                f.UserPower = (int)sdr["UserPower"];
                list.Add(f);
            }
            return list;
        }
        //修改管理员信息
        public static void UpdataMessageName(UserInfo UserInfo)
        {
            string sql = string.Format("update UserInfo set UserPwd='{0}',UserPower='{1}' where UserId={2}", UserInfo.UserPwd, UserInfo.UserPower, UserInfo.UserId);
            DBHelper.Adddata(sql);
        }
        //删除管理员信息
        public static void UpdataAdmin(UserInfo UserInfo)
        {
            string sql = string.Format("update UserInfo set UserState='2' where UserId={0}", UserInfo.UserId);
            DBHelper.Adddata(sql);
        }
        //查询客户管理信息
        public static List<UserInfo> SelectCustomer()
        {
            string sql = string.Format("select*from UserInfo where UserPower='2'");
            SqlDataReader sdr = DBHelper.Getdata(sql);
            List<UserInfo> UI = new List<Entity.UserInfo>();
            while (sdr.Read())
            {
                UserInfo f = new UserInfo();
                f.UserId = (int)sdr["UserId"];
                f.InfoName = (string)sdr["InfoName"];
                f.InfoSex = (int)sdr["InfoSex"];
                f.InfoMail = (string)sdr["InfoMail"];
                f.InfoAddress = (string)sdr["InfoAddress"];
                f.InfoPhone = (string)sdr["InfoPhone"];
                f.UserState = (int)sdr["UserState"];
                UI.Add(f);
            }
            sdr.Close();
            return UI;
        }
        //改变用户状态
        public static void updateCustomer(UserInfo u)
        {
            string sql = string.Format("select * from UserInfo where UserPower='2' and UserId='{0}'", u.UserId);
            SqlDataReader sdr = DBHelper.Getdata(sql);
            int i = 0;
            while (sdr.Read())
            {
                i = (int)sdr["UserState"];
            }
            sdr.Close();
            if (i == 1)
            {
                string sql1 = string.Format("update UserInfo set UserState='2' where UserId={0}", u.UserId);
                DBHelper.Adddata(sql1);
            }
            else if (i == 2)
            {
                string sql1 = string.Format("update UserInfo set UserState='1' where UserId={0}", u.UserId);
                DBHelper.Adddata(sql1);
            }
        }
    }
}